﻿@{
    
    Layout = "~/Shared/Layouts/_AdminLayout.cshtml";
    Page.Title = "Детали на нарачка";
    
    var orderID = !UrlData[0].IsEmpty() ? UrlData[0] : "0";

    var db = Database.Open("VirtualeShop");  

    if (IsPost)
    {
        orderID = Request["orderID"];
        var shipped = Request["shipped"].AsBool();
        
        var sqlUpdate = "UPDATE Orders SET OrderShipped = @0 WHERE OrderId = @1";
        db.Execute(sqlUpdate, shipped, orderID);
        
        Response.Redirect("~/Admin/Orders/");
    } 
    
    // Get Order header details
    var sqlCommand = "SELECT Orders.*, UserProfile.Email FROM Orders " + 
                     "INNER JOIN UserProfile ON Orders.UserId = UserProfile.UserId " +
                     "WHERE OrderId = @0 ";
    
    var order = db.QuerySingle(sqlCommand, orderID);
    
    // Get Order items
    var itemsSql = "SELECT * FROM OrderItems WHERE OrderID = @0";
    var orderItems = db.Query(itemsSql, orderID);
    
}

@if (order == null)
{
    <p>
        Не може да се добијат детали на нарачката.
    </p>
}
else
{
    <text>
        <h1>Детали на нарачка</h1>
        <p><strong>Број на нарачка:</strong> @order.OrderId</p>
        <p><strong>Корисник:</strong> <a href="mailto:@order.Email">@order.Email</a></p>
        <p><strong>Нарачано: </strong>@order.OrderDateTime</p>
        
        <h2>Детали за испорака</h2>
        <p><strong>Адреса 1:</strong> @order.Address1</p>
        <p><strong>Адреса 2:</strong> @order.Address2</p>
        <p><strong>Град: </strong>@order.Town</p>
        <p><strong>Регион:</strong> @order.Region</p>
        <p><strong>Поштенски код:</strong> @order.PostalCode</p>
        <p><strong>Држава: </strong>@order.Country</p>
        <form action="orderDetails" method="post" id="shippedForm">
                @Html.Hidden("orderId", order.OrderId)
                    Испорачана нарачка? 
                    @Html.CheckBox("shipped", order.OrderShipped, new { value = "true" })
                    <input type="submit" value="Обнови" />
            </form>
        <h2>Нарачај производи</h2>
        <table id="cartTable">
            <tr>
                <th class="product">Производ</th>
                <th class="size">Големина</th>
                <th class="price">Цена</th>
            </tr>
            
            @foreach (var item in orderItems)
            {
                <tr>
                    <td class="product">@Products.GetProductNameFromId(item.ProductID)</td>
                    <td class="size">@item.Size</td>
                    <td class="price">£@item.Price</td>
                </tr>
            }
            
            <tr class="cartTotal">
                <td colspan="2">&nbsp;</td>
                <td>Вкупно: £@order.OrderTotal</td>
            </tr>
        </table>
    </text>
}

